package org.jeecg;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import org.apache.http.client.utils.URIBuilder;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

public class Demo {
    private static final String ACCESS_TOKEN = "be57be358c718bdeae6edb68a64b470bd3984bd9";
    private static final String PATH = "/open_api/2/user/info/";

    /**
     * Build request URL
     *
     * @param path Request path
     * @return Request URL
     */
    private static String buildUrl(String path) throws URISyntaxException {
        URI uri = new URI("https", "ad.oceanengine.com", path, "", "");
        return uri.toString();
    }


    /**
     * Send GET request
     *
     * @param jsonStr:Args in JSON format
     * @return Response in JSON format
     */
    private static String get(String jsonStr) throws IOException, URISyntaxException {
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        URIBuilder ub = new URIBuilder(buildUrl(PATH));
//        ObjectMapper mapper = new ObjectMapper();
//        Map<String, Object> map = mapper.readValue(jsonStr, Map.class);
//        map.forEach((k, v) -> {
//            try {
//                ub.addParameter(k, v instanceof String ? (String) v : mapper.writeValueAsString(v));
//            } catch (JsonProcessingException e) {
//                e.printStackTrace();
//            }
//        });
        URL url = ub.build().toURL();

        Request request = new Request.Builder()
                .url(url)
                .method("GET", null)
                .addHeader("Access-Token", ACCESS_TOKEN)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    public static void main(String[] args) throws IOException, URISyntaxException {
        String access_token = ACCESS_TOKEN;

        // Args in JSON format
        System.out.println(get(access_token));
    }
}
